<?php
namespace app\interfaces\controller;

use app\interfaces\model\Information as InformationModel;
use app\interfaces\model\Promotion as PromotionModel;
use app\interfaces\model\ShopGoods as ShopGoodsModel;
use app\interfaces\model\ShopGoodsFlag as ShopGoodsFlagModel;
use think\Cache;
use think\Request;
use think\Response;
use think\View;

class Config
{

    public function qrcodeWithPicture_self($uid = '', $level = 7, $size = 100)

    {

        Loader::import('phpqrcode.phpqrcode',EXTEND_PATH);
        $url = Request::instance()->domain()."/index.php/mobile/user_port/app_login?uid=".$uid;

        // $url = "http://app65.app.longcai.net/index.php/mobile/user_port/login?uid=".$uid;

        //容错级别

        $errorCorrectionLevel = intval($level);

        //生成图片大小

        $matrixPointSize = intval($size);

        //生成二维码图片

        $object = new \QRcode();

        $path = ROOT_PATH . 'public/static/mobile/self/'.$uid.'.png';



        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件

        $object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);

        $content = '<style> img {vertical-align:middle; width:auto;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<img src="/'.PromotionModel::where(['id' => $id])->value('picUrl').'" style="width:100%;display:block" />';
        $content .= PromotionModel::where(['id' => $id])->value('content');

        return Response::create($content, 'html');
//        //准备好的logo图片
//
//        $logo = Request::instance()->domain()."/static/mobile/img_user/qrcode_logo.jpg";
//
//        //已经生成的原始二维码图
//
//        $qrcode = $path;
//
//
//
//        //logo图片存在
//
//        if ($logo !== FALSE) {
//
//            $qrcode = imagecreatefromstring(file_get_contents($qrcode));
//
//            $logo = imagecreatefromstring(file_get_contents($logo));
//
//            $qrcode_width = imagesx($qrcode);   //二维码图片宽度
//
//            $qrcode_height = imagesy($qrcode);  //二维码图片高度
//
//            $logo_width = imagesx($logo);       //logo图片宽度
//
//            $logo_height = imagesy($logo);      //logo图片高度
//
//            $logo_qr_width = $qrcode_width / 5;
//
//            $scale = $logo_width / $logo_qr_width;
//
//            $logo_qr_height = $logo_height / $scale;
//
//            $from_width = ($qrcode_width - $logo_qr_width) / 2;
//
//            //重新组合图片并调整大小[生成中间带logo的二维码  $qrcode]
//
//            imagecopyresampled($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
//
//
//
//        }
//
//        //2.输出二维码图片
//
//        imagepng($qrcode, $path);
//
//        //背景图片
//
//        $bgImg = ROOT_PATH . "public/static/mobile/img_user/wap_share.png";
//
//        $bgImg = imagecreatefromstring(file_get_contents($bgImg));
//
//        $bgWidth = imagesx($bgImg);       //背景图片宽度
//
//        $bgHight = imagesy($bgImg);      //背景图片高度
//
//        $code_width = imagesx($qrcode)*0.85;       //二维码图片宽度
//
//        $code_height = imagesy($qrcode)*0.85;      //二维码图片高度
//
//        $bg_from_width = ($bgWidth - $code_width) / 2;
//
//        //重新组合图片并调整大小[二维码和背景图片组合  $bgImg]
//
//        imagecopyresampled($bgImg, $qrcode, $bg_from_width*1.015, $bg_from_width*3.685, 0, 0, $code_width, $code_height, imagesx($qrcode), imagesy($qrcode));
//
//        //三合一图片
//
//        imagepng($bgImg, $path);

    }

    /*
     * 杂项 - 促销详情web
     */
    public function promotion_view($id)
    {

        $content = '<style> img {vertical-align:middle; width:auto;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<img src="/'.PromotionModel::where(['id' => $id])->value('picUrl').'" style="width:100%;display:block" />';
        $content .= PromotionModel::where(['id' => $id])->value('content');

        return Response::create($content, 'html');

    }

    /*
     * 杂项 - 商家端 - 帮助中心列表
     */
    public function shop_info_list()
    {

        $data = InformationModel::where(['parentStr' => ['like', '%,67,%']])->field('id,title')->select();

        return ['code' => 200, 'message' => '成功', 'data' => $data];
    }

    /*
     * 杂项 - 商家端 - 帮助中心web
     */
    public function shop_info_view($id)
    {

        $content = '<style> img {vertical-align:middle; width:auto; max-width:95%;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= InformationModel::where(['id' => $id])->value('content');

        return Response::create($content, 'html');

    }

    /*
     * 杂项 - 商家端 - 服务条款web or  平台管理协议web
     */
    public function service_view($id)
    {

        $content = '<style> img {vertical-align:middle; width:auto; max-width:95%;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= InformationModel::where(['parentStr' => ['like', '%,' . $id . ',%']])->value('content');

        return Response::create($content, 'html');

    }

    /*
     * 杂项 - 用户端 - 帮助中心列表
     */
    public function info_list()
    {

        $data = InformationModel::where(['parentStr' => ['like', '%,1,%']])->field('id,title')->select();

        return ['code' => 200, 'message' => '成功', 'data' => $data];
    }

    /*
     * 杂项 - 用户端 - 帮助中心web
     */
    public function info_view($id)
    {

        $content = InformationModel::where(['id' => $id])->value('content');

        $view = new View();
        echo $view->fetch('',['data'=>$content]);

    }

    /*
     * 杂项 - 服务电话号码
     */
    public function service_phone()
    {
        return ['code' => 200, 'message' => '成功', 'data' => Cache::get('web_customer_service')];
    }

    /*
     * 杂项 - 当前版本号
     */
    public function version_number()
    {
        return ['code' => 200, 'message' => '成功', 'data' => Cache::get('web_version')];
    }

    /*
      * 杂项 - 商品详情web
      */
    public function goods_view($id)
    {

        $content = '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= ShopGoodsModel::where(['id' => $id])->value('content');

        return Response::create($content, 'html');

    }
    /*
     * 杂项 - 商品详情web
     */
    public function goods_view1($id)
    {

//        $content = '<style> img {vertical-align:middle; width:auto; max-width:100%;} body{position: absolute;width: 100%;height: 100%;}</style>';
//        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
//        $content .= '<meta content="yes" name="apple-mobile-web-app-capable"/>';
//        $content .= '<meta content="black" name="apple-mobile-web-app-status-bar-style"/>';
//        $content .= '<script src="/static/mobile/js/jquery-1.8.2.js"></script>';
//        $content .= '<script>alert($("body").height())</script>';
//        $content .= ShopGoodsModel::where(['id' => $id])->value('content');
//
//        return Response::create($content, 'html');
//        $content = '<style> img {vertical-align:middle; width:auto; max-width:100%;} body{position: absolute;width: 100%;height: 100%;}</style>';
//        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
//        $content .= '<meta content="yes" name="apple-mobile-web-app-capable"/>';
//        $content .= '<meta content="black" name="apple-mobile-web-app-status-bar-style"/>';
//        $content .= ShopGoodsModel::where(['id' => $id])->value('content');
        $height = Request::instance()->param('height');
        $data = ShopGoodsModel::where(['id' => $id])->value('content');
        $view = new View();

        echo $view->fetch('', ['data' => $data,'height' => $height]);
//        return Response::create($content, 'html');
    }

    public function height(){
        $height = Request::instance()->param('height');
        $id = Request::instance()->param('id');
        if($height && $id){
            Cache::set('c_height',$height);
            Cache::set('c_id',$id);
        }
        if($id && !$height){
           $c_height= Cache::get('c_height');
           $c_id =  Cache::get('c_id');
            return ['code' => 200,'height' => $c_height.'px','id' => $c_id];
        }

    }

    /*
     * 杂项 - 商品规格web
     */
    public function goods_spec($id)
    {

        $data = ShopGoodsFlagModel::where(['goods_id' => $id])->select();

        $view = new View();

        echo $view->fetch('', ['data' => $data]);

    }

}
